<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../header.jsp" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
	.main_div{
		margin : 15px;
	}
</style>
</head>
<body>
<div class="main_div">
	<form id="form_insert" onsubmit="return false" class="layui-form layui-form-pane" action="">
		<div class="layui-form-item">
			<label class="layui-form-label">物料名称</label>
			<div class="layui-input-block">
				<input type="text" id="name" name="name" lay-verify="required" placeholder="请输入物料名称" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">物料规格</label>
			<div class="layui-input-block">
				<input type="text" id="norms" name="norms" lay-verify="required" placeholder="请输入规格" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">物料类型</label>
			<div class="layui-input-block" id="types" name="types">
			</div>
			<input type="button" class="layui-btn" onclick="submitForm()" value="提交"/>
		</div>
	</form>


	<script>
		layui.config({
			base : '/static/'
		}).extend({
			selectM: './layui_extends/selectM'
		}).use(['form', 'selectM'], function () {
			var form = layui.form;
			var selectM = layui.selectM;

			$.ajax({
				url: '/type/selectAllTypes',
				type: 'POST',
				dataType: 'json',
				success: function (jsonResult) {
					if (jsonResult.ok) {
						//多选标签-基本配置
						var tagIns = selectM({
							//元素容器【必填】
							elem: '#types'
							//候选数据【必填】
							, data: jsonResult.data // List<LayUISelectMVO>
							//默认值
							/* ,selected: [1,2] */
							// 默认最多选中5个
							, max: 100
						});
					}
				}
			});
		});

		//ajax方式提交form表单
		function submitForm() {
			// 使用正则表达式验证表单数据是否合法
			// 用户名 6-10字母数字
			// 邮箱：要符合邮箱的格式

			$.post(
					'/material/insert',
					$('#form_insert').serialize(),
					function(jsonResult) {
						if (jsonResult.code == 0) {
							// 获得当前弹出框的index
							var index = parent.layer.getFrameIndex(window.name);
							layer.msg(
									jsonResult.msg,
									{icon:1, time:1000},
									function() { // msg弹出1秒后消失触发这个函数
										// 关闭弹出层
										parent.layer.close(index);
										// 刷新父页面
										window.parent.location.reload();
									}
							);
						} else {
							mylayer.errorMsg()
						}
					},
					'json'
			);
		}

	</script>
</div>
</body>
</html>